package datasources.flipdatasource;

import java.io.UnsupportedEncodingException;
import java.util.Vector;

import org.jopac2.utils.BookSignature;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import bookhive.datastructures.datasource.HashItem;

public class FlipHashQueryContentHandler extends DefaultHandler {
	
	private Vector<HashItem> records;
	private boolean recording;
	private long currentid = 0;
	private String currenttype;
	private String currenthash;
	private Vector<BookSignature> currentsigns = null;
	private BookSignature currentsign = null;
	private String currentlibraryID;
	private String currentlibraryName;
	private String currentidNumber;
	private String currentlocalization;
	private String currentcons;
	
	private StringBuffer buffer = new StringBuffer();
	
	public FlipHashQueryContentHandler(){
		records = new Vector<HashItem>();
		recording = false;
	}
	
	public Vector<HashItem> getRecords(){
		return records;
	}

	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		
		if(recording){
			String a = new String(arg0, arg1, arg2);
			buffer = buffer.append(a);//java.net.URLDecoder.decode(a, "UTF-8");

		}
		
	}

	
	public void endElement(String arg0, String arg1, String arg2) throws SAXException {
		
		
		if(!recording) return;
		
		String string = "";
		try {
			string = java.net.URLDecoder.decode(buffer.toString(), "UTF-8");
		} catch (UnsupportedEncodingException e) { e.printStackTrace(); }
		buffer = new StringBuffer();
		if(arg2.equals("id"))
			currentid = Long.parseLong(string);
		else if(arg2.equals("type"))
			currenttype = string;
		else if(arg2.equals("hash"))
			currenthash = string;
		else if(arg2.equals("libraryId"))
			currentlibraryID = string;
		else if(arg2.equals("libraryName"))
			currentlibraryName = string;
		else if(arg2.equals("idNumber"))
			currentidNumber = string;
		else if(arg2.equals("localization"))
			currentlocalization = string;
		else if(arg2.equals("cons"))
			currentcons = string;
		else if(arg2.equals("signature")){
			if(currentcons == null || currentcons.length()<1)
				currentsign = new BookSignature(currentlibraryID, currentlibraryName, currentidNumber, currentlocalization);
			else
				currentsign = new BookSignature(currentlibraryID, currentlibraryName, currentidNumber, currentlocalization, currentcons);
			
			if(currentsign!=null){
				currentsigns.add(currentsign);
			}
		}
			
			
		else if(arg2.equals("resultSet"))
			recording = false;
		else if (arg2.equals("record")){
			HashItem hi = new HashItem();
			hi.id = currentid;
			hi.type = currenttype;
			hi.hash = currenthash;
			hi.signs.addAll(currentsigns);
			records.add(hi);
		}

	}

	
	public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if(arg2.equals("resultSet"))
			recording = true;
		else if(arg2.equals("record")){
			currentid = 0;
			currenttype = null;
			currenthash = null;
			currentsigns = null;
			currentsign = null;
			currentlibraryID = null;
			currentlibraryName = null;
			currentidNumber = null;
			currentlocalization = null;
			currentcons = null;
		}
		else if(arg2.equals("signatures"))
			currentsigns = new Vector<BookSignature>();
	}

	
	
}
